/* Copyright (c) 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.gdata.aeuploader;


import java.io.IOException;
import java.io.InputStream;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreInputStream;

/**
 * The Class AppEngineBlobProvider.
 */
public class AppEngineBlobProvider implements BlobProvider {

  /** The key. */
  private BlobKey key;
  
  /** The info. */
  private BlobInfo info;
  
  /** The factory. */
  private BlobInfoFactory factory;
  
  /**
   * Instantiates a new app engine blob provider.
   *
   * @param blobKey the blob key
   */
  public AppEngineBlobProvider(BlobKey blobKey) {
    key = blobKey;
    factory = new BlobInfoFactory();
    info = factory.loadBlobInfo(key);
  }
  
  /* (non-Javadoc)
   * @see com.google.gdata.aeuploader.BlobProvider#getSize()
   */
  @Override
  public long getSize() {
    return info.getSize();
  }

  /* (non-Javadoc)
   * @see com.google.gdata.aeuploader.BlobProvider#getStream()
   */
  @Override
  public InputStream getStream() {
    try {
      return new BlobstoreInputStream(key);
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
  }

  /* (non-Javadoc)
   * @see com.google.gdata.aeuploader.BlobProvider#getType()
   */
  @Override
  public String getType() {
    return info.getContentType();
  }

}
